package com.bjsxt.common.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.users.pojo.AAdmins;
import com.bjsxt.users.service.IUsersService;

/**
 * 存放不需要登陆的操作
 * @author wangshSxt
 *
 */
@Controller
public class NoLoginController extends BaseController
{
	@Resource
	private IUsersService usersService; 
	
	/**
	 * 打开管理员登陆页面
	 * @return
	 */
	@RequestMapping("/adminsLogin.htm")
	public String adminsLogin(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		if(admins != null)
		{
			/* 说明已经登陆 */
			/* 页面跳转(客户端) */
			return "redirect:/back/admins/index.htm" ; 
		}
		return "/back/adminsLogin";
	}
	
	/**
	 * 打开管理员登陆提交页面
	 * @return
	 */
	@RequestMapping("/adminsLoginSubmit.htm")
	public String adminsLoginSubmit(HttpServletRequest request,String email,String password,String code)
	{
		HttpSession session = request.getSession();
		/*
		 * 登陆成功,跳转到/index.htm页面
		 * 登陆失败:跳转到/adminsLogin.htm页面
		 * */
		/*
		 * 接收参数
		 * 处理参数
		 * 根据emails查询管理员(数据库)
		 * 做判断
		 * 登陆成功,跳转到/index.htm页面
		 * 登陆失败:跳转到/adminsLogin.htm页面
		 * */
		
		/* 从session中取验证码 */
		String codeSess = session.getAttribute("code") + "";
		/* 将当前验证码从session中移除
		 * 登陆成功或者登陆失败(并且抛出异常):验证码木有重新加载;session中的验证码可以重复利用
		 * 
		 * 移除验证码(可以session销毁吗?)
		 * session.invalidate();
		 *  */
		session.removeAttribute("code");
		if(codeSess != null && codeSess.equalsIgnoreCase(code))
		{
			/*接收参数 属性驱动 */
			Map<String, Object> condMap = new HashMap<String, Object>();
			/* 如果email:为空(null),会查询出多条结果 */
			condMap.put("email", email);
			AAdmins admins = this.usersService.findOneAdminsService(condMap);
			if(admins != null)
			{
				//用户名正常
				if(this.encryptUtil.checkPassword(password, admins.getPassword()))
				{
					//密码正确
					if(admins.getStatus() == 1)
					{
						//登陆成功
						/*
						 * 管理员对象(session)
						 * 上次登陆时间(session)
						 * 
						 * 更新上次登陆时间
						 * 
						 * 跳转页面
						 * */
						session.setAttribute("admins", admins);
						session.setAttribute("lastLoginTime", admins.getLastLoginTime());
						
						admins.setLastLoginTime(new Date());
						this.usersService.updateOneAdminsService(admins);
						
						/* 页面跳转(客户端) */
						return "redirect:/back/admins/index.htm" ; 
					}else
					{
						this.info = "账户被禁用" ; 
					}
				}else
				{
					this.info = "密码不正确" ;
				}
				request.setAttribute("admins", admins);
			}else
			{
				//邮箱不存在
				this.info = "邮箱不存在" ; 
			}
		}else
		{
			this.info = "验证码不正确" ; 
		}
		request.setAttribute("info", info);
		return this.adminsLogin(request) ; 
	}
}
